package tum0r.data.algorithm;

import tum0r.misc.ArrayUtils;

/**
 * 工程: JavaTools<br>
 * 包: tum0r.data.algorithm<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/8/22 20:20<br>
 * <br>
 */
public class FastSort<T extends Comparable<T>> {

    public void sort(T[] array, int start, int stop) {
        if (ArrayUtils.isNullOrEmpty(array) || start < 0 || stop < 0 || stop - start <= 1) {
            return;
        }
        int left = start;
        int right = stop - 1;
        int now = right;
        T temp;
        while (left <= right) {
            if (now == left - 1) {
                if (array[now].compareTo(array[right]) > 0) {
                    temp = array[now];
                    array[now] = array[right];
                    array[right] = temp;
                    now = right;
                }
                right--;
            } else {
                if (array[now].compareTo(array[left]) < 0) {
                    temp = array[now];
                    array[now] = array[left];
                    array[left] = temp;
                    now = left;
                }
                left++;
            }
        }
        sort(array, start, now);
        sort(array, now + 1, stop);
    }
}
